{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 基本数据类型\n",
    "\n",
    "- Number（数字）\n",
    "  - int：python的整数只受内存大小限制，不容易溢出\n",
    "  - float：浮点数存储近似值，精度为15位有效数字\n",
    "  - bool：在 Python 中，所有非零的数字和非空的字符串、列表、元组等数据类型都被视为 True，只有**0、空字符串、空列表、空元组**等被视为 False。因此，在进行布尔类型转换时，需要注意数据类型的真假性。\n",
    "  - complex（复数）\n",
    "- 序列类型\n",
    "  - List（列表）\n",
    "  - Tuple（元组）\n",
    "- 其他类型\n",
    "\t- Set（集合）\n",
    "\t- Dictionary（字典）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 输入\n",
    "# eval函数\n",
    "a, b = eval(input()) # 可以接受类似于1, 2的输入。因为eval函数会将输入的字符串当作Python表达式来计算。\n",
    "# 会去掉字符串的引号，去除后输入就是一个元组"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "可迭代 & 不可迭代！！"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 字符串"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "| 序号 | 方法及描述                                                   |\n",
    "| :--- | :----------------------------------------------------------- |\n",
    "| 1    | [capitalize()](https://www.runoob.com/python3/python3-string-capitalize.html) 将字符串的第一个字符转换为大写 |\n",
    "| 2    | [center(width, fillchar)](https://www.runoob.com/python3/python3-string-center.html)返回一个指定的宽度 width 居中的字符串，fillchar 为填充的字符，默认为空格。 |\n",
    "| 3    | [count(str, beg= 0,end=len(string))](https://www.runoob.com/python3/python3-string-count.html) 返回 str 在 string 里面出现的次数，如果 beg 或者 end 指定则返回指定范围内 str 出现的次数 |\n",
    "| 4    | [bytes.decode(encoding=\"utf-8\", errors=\"strict\")](https://www.runoob.com/python3/python3-string-decode.html) Python3 中没有 decode 方法，但我们可以使用 bytes 对象的 decode() 方法来解码给定的 bytes 对象，这个 bytes 对象可以由 str.encode() 来编码返回。 |\n",
    "| 5    | [encode(encoding='UTF-8',errors='strict')](https://www.runoob.com/python3/python3-string-encode.html) 以 encoding 指定的编码格式编码字符串，如果出错默认报一个ValueError 的异常，除非 errors 指定的是'ignore'或者'replace' |\n",
    "| 6    | [endswith(suffix, beg=0, end=len(string))](https://www.runoob.com/python3/python3-string-endswith.html) 检查字符串是否以 suffix 结束，如果 beg 或者 end 指定则检查指定的范围内是否以 suffix 结束，如果是，返回 True,否则返回 False。 |\n",
    "| 7    | [expandtabs(tabsize=8)](https://www.runoob.com/python3/python3-string-expandtabs.html) 把字符串 string 中的 tab 符号转为空格，tab 符号默认的空格数是 8 。 |\n",
    "| 8    | [find(str, beg=0, end=len(string))](https://www.runoob.com/python3/python3-string-find.html) 检测 str 是否包含在字符串中，如果指定范围 beg 和 end ，则检查是否包含在指定范围内，如果包含返回开始的索引值，否则返回-1 |\n",
    "| 9    | [index(str, beg=0, end=len(string))](https://www.runoob.com/python3/python3-string-index.html) 跟find()方法一样，只不过如果str不在字符串中会报一个异常。 |\n",
    "| 10   | [isalnum()](https://www.runoob.com/python3/python3-string-isalnum.html) 检查字符串是否由字母和数字组成，即字符串中的所有字符都是字母或数字。如果字符串至少有一个字符，并且所有字符都是字母或数字，则返回 True；否则返回 False。 |\n",
    "| 11   | [isalpha()](https://www.runoob.com/python3/python3-string-isalpha.html) 如果字符串至少有一个字符并且所有字符都是字母或中文字则返回 True, 否则返回 False |\n",
    "| 12   | [isdigit()](https://www.runoob.com/python3/python3-string-isdigit.html) 如果字符串只包含数字则返回 True 否则返回 False.. |\n",
    "| 13   | [islower()](https://www.runoob.com/python3/python3-string-islower.html) 如果字符串中包含至少一个区分大小写的字符，并且所有这些(区分大小写的)字符都是小写，则返回 True，否则返回 False |\n",
    "| 14   | [isnumeric()](https://www.runoob.com/python3/python3-string-isnumeric.html) 如果字符串中只包含数字字符，则返回 True，否则返回 False |\n",
    "| 15   | [isspace()](https://www.runoob.com/python3/python3-string-isspace.html) 如果字符串中只包含空白，则返回 True，否则返回 False. |\n",
    "| 16   | [istitle()](https://www.runoob.com/python3/python3-string-istitle.html) 如果字符串是标题化的(见 title())则返回 True，否则返回 False |\n",
    "| 17   | [isupper()](https://www.runoob.com/python3/python3-string-isupper.html) 如果字符串中包含至少一个区分大小写的字符，并且所有这些(区分大小写的)字符都是大写，则返回 True，否则返回 False |\n",
    "| 18   | [join(seq)](https://www.runoob.com/python3/python3-string-join.html) 以指定字符串作为分隔符，将 seq 中所有的元素(的字符串表示)合并为一个新的字符串 |\n",
    "| 19   | [len(string)](https://www.runoob.com/python3/python3-string-len.html) 返回字符串长度 |\n",
    "| 20   | [ljust(width[, fillchar\\])](https://www.runoob.com/python3/python3-string-ljust.html) 返回一个原字符串左对齐,并使用 fillchar 填充至长度 width 的新字符串，fillchar 默认为空格。 |\n",
    "| 21   | [lower()](https://www.runoob.com/python3/python3-string-lower.html) 转换字符串中所有大写字符为小写. |\n",
    "| 22   | [lstrip()](https://www.runoob.com/python3/python3-string-lstrip.html) 截掉字符串左边的空格或指定字符。 |\n",
    "| 23   | [maketrans()](https://www.runoob.com/python3/python3-string-maketrans.html) 创建字符映射的转换表，对于接受两个参数的最简单的调用方式，第一个参数是字符串，表示需要转换的字符，第二个参数也是字符串表示转换的目标。 |\n",
    "| 24   | [max(str)](https://www.runoob.com/python3/python3-string-max.html) 返回字符串 str 中最大的字母。 |\n",
    "| 25   | [min(str)](https://www.runoob.com/python3/python3-string-min.html) 返回字符串 str 中最小的字母。 |\n",
    "| 26   | [replace(old, new [, max\\])](https://www.runoob.com/python3/python3-string-replace.html) 把 将字符串中的 old 替换成 new,如果 max 指定，则替换不超过 max 次。 |\n",
    "| 27   | [rfind(str, beg=0,end=len(string))](https://www.runoob.com/python3/python3-string-rfind.html) 类似于 find()函数，不过是从右边开始查找. |\n",
    "| 28   | [rindex( str, beg=0, end=len(string))](https://www.runoob.com/python3/python3-string-rindex.html) 类似于 index()，不过是从右边开始. |\n",
    "| 29   | [rjust(width,[, fillchar\\])](https://www.runoob.com/python3/python3-string-rjust.html) 返回一个原字符串右对齐,并使用fillchar(默认空格）填充至长度 width 的新字符串 |\n",
    "| 30   | [rstrip()](https://www.runoob.com/python3/python3-string-rstrip.html) 删除字符串末尾的空格或指定字符。 |\n",
    "| 31   | [split(str=\"\", num=string.count(str))](https://www.runoob.com/python3/python3-string-split.html) 以 str 为分隔符截取字符串，如果 num 有指定值，则仅截取 num+1 个子字符串 |\n",
    "| 32   | [splitlines([keepends\\])](https://www.runoob.com/python3/python3-string-splitlines.html) 按照行('\\r', '\\r\\n', \\n')分隔，返回一个包含各行作为元素的列表，如果参数 keepends 为 False，不包含换行符，如果为 True，则保留换行符。 |\n",
    "| 33   | [startswith(substr, beg=0,end=len(string))](https://www.runoob.com/python3/python3-string-startswith.html) 检查字符串是否是以指定子字符串 substr 开头，是则返回 True，否则返回 False。如果beg 和 end 指定值，则在指定范围内检查。 |\n",
    "| 34   | [strip([chars\\])](https://www.runoob.com/python3/python3-string-strip.html) 在字符串上执行 lstrip()和 rstrip() |\n",
    "| 35   | [swapcase()](https://www.runoob.com/python3/python3-string-swapcase.html) 将字符串中大写转换为小写，小写转换为大写 |\n",
    "| 36   | [title()](https://www.runoob.com/python3/python3-string-title.html) 返回\"标题化\"的字符串,就是说所有单词都是以大写开始，其余字母均为小写(见 istitle()) |\n",
    "| 37   | [translate(table, deletechars=\"\")](https://www.runoob.com/python3/python3-string-translate.html) 根据 table 给出的表(包含 256 个字符)转换 string 的字符, 要过滤掉的字符放到 deletechars 参数中 |\n",
    "| 38   | [upper()](https://www.runoob.com/python3/python3-string-upper.html) 转换字符串中的小写字母为大写 |\n",
    "| 39   | [zfill (width)](https://www.runoob.com/python3/python3-string-zfill.html) 返回长度为 width 的字符串，原字符串右对齐，前面填充0 |\n",
    "| 40   | [isdecimal()](https://www.runoob.com/python3/python3-string-isdecimal.html) 检查字符串是否只包含十进制字符，如果是返回 true，否则返回 false。 |"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'gfedcba'"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "str = 'abcdefg'\n",
    "len(str)\n",
    "# 翻转字符串\n",
    "str[::-1]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "str.strip('fg') # 删除字符串两端的指定字符（只能删除两端，不能删除中间，会删除所有f和g）"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![字符串索引](https://static.jyshare.com/wp-content/uploads/123456-20200923-1.svg)\n",
    "\n",
    "注意：截取字符串遵循左闭右开原则（python中的range()也是左闭右开）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 提取相隔n个字符的字符\n",
    "def extract_substring(s, n):\n",
    "    \"\"\"\n",
    "    提取字符串s中每隔n个字符的字符\n",
    "    \"\"\"\n",
    "    return s[n-1::n]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![](./image/python字符串运算符.jpg)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'dfhj'"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 字符串切片：双冒号\n",
    "# 语法为str[start:end:step]\n",
    "\n",
    "str = 'abcdefghijklmnopqrstuvwxyz'\n",
    "str[::4]  # 每隔4个字符提取一个字符\n",
    "str[3::4]  # 从第4个字符开始，每隔4个字符提取一个字符\n",
    "str[3:10:2]  # 从第4个字符到第10个字符，每隔2个字符提取一个字符\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 字符串类型不支持后期单独对某个元素赋值，除非对整个字符串重新赋值\n",
    "# 例如练习4选择题第1题\n",
    "\n",
    "str = \"hello, world!\"\n",
    "str[5] = 'H'\n",
    "\n",
    "'''\n",
    "TypeError: 'str' object does not support item assignment\n",
    "'''"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# *解包\n",
    "\n",
    "# 1. 解包位置参数，使得函数可以接受不定数量的参数\n",
    "def add(*args):\n",
    "    result = 0\n",
    "    for num in args:\n",
    "        result += num\n",
    "    return result\n",
    "print(add(1, 2, 3))  # 输出 6\n",
    "\n",
    "# 2. **用于解包关键字参数，将它们打包成一个字典\n",
    "def person_info(**kwargs):\n",
    "    for key, value in kwargs.items():\n",
    "        print(f\"{key}: {value}\")\n",
    "\n",
    "person_info(name=\"Alice\", age=30, country=\"USA\")\n",
    "'''\n",
    "输出：\n",
    "name: Alice\n",
    "age: 30\n",
    "country: USA\n",
    "'''\n",
    "\n",
    "# 3. *扩展列表\n",
    "list1 = [1, 2, 3]\n",
    "list2 = [4, 5, 6]\n",
    "list1.extend(list2)\n",
    "print(list1)  # 输出 [1, 2, 3, 4, 5, 6]\n",
    "\n",
    "# 使用 * 扩展列表\n",
    "list1 = [1, 2, 3]\n",
    "list2 = [4, 5, 6]\n",
    "list1 = [*list1, *list2]\n",
    "print(list1)  # 输出 [1, 2, 3, 4, 5, 6]\n",
    "\n",
    "# 4. **扩展字典\n",
    "dict1 = {\"name\": \"Alice\", \"age\": 30}\n",
    "dict2 = {\"country\": \"USA\"}\n",
    "dict1.update(dict2)\n",
    "print(dict1)\n",
    "# 输出：{'name': 'Alice', 'age': 30, 'country': 'USA'}\n",
    "\n",
    "# 使用 ** 扩展字典\n",
    "dict1 = {\"name\": \"Alice\", \"age\": 30}\n",
    "dict2 = {\"country\": \"USA\"}\n",
    "dict1 = {**dict1, **dict2}\n",
    "print(dict1)\n",
    "# 输出：{'name': 'Alice', 'age': 30, 'country': 'USA'}\n",
    "\n",
    "# 5. *解包元组\n",
    "fruits = (\"apple\", \"banana\", \"cherry\")\n",
    "a, b, c = fruits\n",
    "print(a, b, c)  # 输出 \"apple banana cherry\"\n",
    "\n",
    "# 6. **解包字典中的键值对\n",
    "info = {\"name\": \"Alice\", \"age\": 30}\n",
    "person_info(**info)  # 传递字典作为关键字参数\n",
    "# 输出：\n",
    "# Name: Alice\n",
    "# Age: 30"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 字典"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 输出"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hellofg\n",
      "hello, world!\n"
     ]
    }
   ],
   "source": [
    "# 换行\\n（）与回车\\r（光标回到当行开头）\n",
    "print(str, end='\\r')\n",
    "print(\"hello\")\n",
    "\n",
    "# 分隔符、结束符\n",
    "print(\"hello\", \"world\", sep=\", \", end=\"!\\n\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 格式化输出\n",
    "'''\n",
    "%s 字符串，采用str()显示\n",
    "%d 十进制整数\n",
    "%f 浮点数\n",
    "'''"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": ".venv",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.13.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
